/*

Stickman Adventure - a game created by Daco & his cousin

Copyright (C) 2011-2012  DacoTaco & Wouterke

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


*/

#include "MovingObject.h"

//constructor MovingObject with sound
MovingObject::MovingObject(const char* spritesheet,int xpos , int ypos ,int h,int w ,char typeObj ,const char* sound[] ,int soundlength ):Object(spritesheet, xpos , ypos , h, w, typeObj,sound,soundlength)
{		
	initMovingObject();
}
//constructor MovingObject w/o sound
MovingObject::MovingObject(const char* spritesheet,int xpos , int ypos ,int h,int w ,char typeObj ):Object(spritesheet, xpos , ypos , h, w, typeObj)
{		
	initMovingObject();
}

//initializer is used 2 help construct the movingobject in the constructers
void MovingObject::initMovingObject(){
	status=0;
	velocity = 0;
	yv=1;
	dead = 0;
	isdown = false;
}

MovingObject::~MovingObject()
{

}
char MovingObject::Update(int collision, int y_ground, int x_wall)
{
	char ret = 1;

	if(yv!=DEFAULT_YV)
	{
		if(status<STATUS_HOP_JUMP)status+=STATUS_HOP_JUMP;
		if(yv > -JUMP_YV+1) //cap fallrate at -30, the complete opposite of max jump speed
			yv-=FALL_RATE;
		//gprintf("y+height & yv = %d & %d\n",y+height,yv);
		{
			if( !(collision & 8) )
			{
				//fix in air of regular jump so we dont fall trough the floor.
				//this also prevents us from falling when there is a hole in the floor. fail :( 
				//the problem we have is that if we jump, the -yv can make our y underneeth the object, making collision fail. causing us to keep falling
				//but this at the same time makes it not possible to fall trough a hole in the floor;

				//a possible fix is to max the fallrate at -30 and make floors no thinner then 30 pixels?

				/*if( y_ground > 0 && (y+height - yv) > y_ground  )
				{
					y = y_ground - height;
					yv = DEFAULT_YV;
					if(status<STATUS_HOP_DOWN)
						status-=STATUS_HOP_JUMP;
					else 
						status-=STATUS_HOP_JUMP;
				}
				else*/
				{
					y-=yv;
					ret = 1;
				}
			}
			if( (collision & 8) )
			{	
				if(yv < 0)
				{
					//falling near ground of an object. lets finish the jump... :)
					if( y_ground > 0 && (y+height - yv) > y_ground )
					{
						y = y_ground - height;
						yv = DEFAULT_YV;
						if(status<STATUS_HOP_DOWN)
							status-=STATUS_HOP_JUMP;
						else 
							status-=STATUS_HOP_DOWN;
						ret = 1;
					}
					else
					{
						//keep going :)
						y-=yv;
					}
				}
				/*else
				{
					//reset. while landing we hit a platform. this seems to prevent us from jumping tho :s
					yv = DEFAULT_YV;
					status -= STATUS_HOP_JUMP;
				}*/
			}
			if( (collision & 16) && yv > 0 )
			{
				yv = 0;
			}
		}
	}
	//we are dropping off an object
	else if( !(collision & 8) )
	{
		yv = FALL_RATE;	
		status += STATUS_HOP_JUMP;
	}
	if(!isdown){
		if(status>=STATUS_HOP_DOWN && velocity==0)status-=STATUS_HOP_DOWN;
		if(velocity!=0 && status <STATUS_HOP_JUMP){
			ObjectWalkDelay += 0.1f;
			if (ObjectWalkDelay > OBJECT_WALK_ANIMATION_DELAY) {
			   ObjectWalkDelay = 0.0f;
			   frame++;
			}
			if (frame==SHEET_FRAMES) frame = 0;			
		}
		if (
			velocity < 0 && !(collision & 2) ||
			velocity > 0 && !(collision & 4) )
		{
			if(velocity < 0 && x+velocity < x_wall)
				x = x_wall;
			else if	(velocity > 0 && x+width+velocity > x_wall)
				x = x_wall - width;
			else
				x+=velocity;
			ret = 1;
		}
	}
	else if(status<STATUS_HOP_JUMP)status+=STATUS_HOP_DOWN;

	return ret;
}
int MovingObject::GetVelocity(void)
{
	return velocity;
}
void MovingObject::death(void)
{
}
char MovingObject::IsDead(void)
{
	return dead;
}